package cn.kgc.web.servlet;

import cn.hutool.json.JSONUtil;
import cn.kgc.web.entity.Emp;
import cn.kgc.web.service.EmpService;
import cn.kgc.web.service.impl.EmpServiceImpl;
import cn.kgc.web.utils.ResponseResult;
import cn.kgc.web.utils.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: 课工场
 * @since 2023/9/27
 * 员工业务模块的控制层
 * 1.接受请求
 * 2.做出响应
 * <p>
 * localhost:8080/web/emp?method=addEmp
 * localhost:8080/web/emp?method=select
 * localhost:8080/web/emp?method=update
 * localhost:8080/web/emp?method=delete
 */
@WebServlet("/emp")
public class EmpServlet extends BaseServlet {

    private EmpService empService = new EmpServiceImpl();

    //添加用户
    protected void addEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取参数
        String ename = req.getParameter("ename");
        String age = req.getParameter("age");
        String deptno = req.getParameter("deptno");

        Emp emp = new Emp();
        emp.setAge(Integer.valueOf(age));
        emp.setDeptno(Integer.valueOf(deptno));
        emp.setEname(ename);

        ResponseResult responseResult = empService.addEmp(emp);

        // 如何将responseResult对象以json格式进行返回
        //  java   - >  json字符串    轻量级的数据传输格式  {name:'tom',age:30}  JSON.parse()  JSON.stringify()
        //String result = JSONUtil.toJsonStr(responseResult);
        //// 设置响应格式  json
        //resp.setContentType("application/json;charset=utf-8");
        //PrintWriter writer = resp.getWriter();
        //writer.write(result);
        //writer.close();

        ResponseUtil.writeJson(responseResult, resp);

    }


    // 修改员工信息
    protected void updateEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String ename = req.getParameter("ename");
        String age = req.getParameter("age");
        String deptno = req.getParameter("deptno");
        String id = req.getParameter("id");

        Emp emp = new Emp();

        if (age != null) {
            emp.setAge(Integer.valueOf(age));
        }
        if (deptno != null) {
            emp.setDeptno(Integer.valueOf(deptno));
        }

        if (id != null) {
            emp.setId(Integer.valueOf(id));
        }

        emp.setEname(ename);

        ResponseResult responseResult = empService.updateEmp(emp);
        ResponseUtil.writeJson(responseResult, resp);

    }

    // 删除员工信息
    protected void deleteEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("id");

        Integer eid = null;
        if (id != null) {
            eid = Integer.valueOf(id);
        }

        ResponseResult responseResult = empService.deleteById(eid);
        ResponseUtil.writeJson(responseResult, resp);
    }


    // 查询员工信息列表
    protected void selectEmps(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));
        String ename = req.getParameter("ename");


        ResponseResult responseResult = empService.empPage(page, limit,ename);
        ResponseUtil.writeJson(responseResult, resp);

    }


}
